# _*_ coding:utf-8 _*_
# @Time  : 2022.06.21
# @Author: zizlee
import hashlib
import random
import re
from itertools import groupby


def strQ2B(ustring):
    """ 全角转半角 """
    rstring = ""
    for uchar in ustring:
        inside_code = ord(uchar)
        if inside_code == 12288:  # 全角空格直接转换
            inside_code = 32
        elif 65281 <= inside_code <= 65374:  # 全角字符（除空格）根据关系转化
            inside_code -= 65248
        else:
            pass
        rstring += chr(inside_code)
    return rstring


def split_number_en(ustring):
    return [''.join(list(g)) for k, g in groupby(ustring, key=lambda x: x.isdigit())]


def generate_random_string(isdigit=False, isalpha=False, isupper=True, alluper=False, ismix=True, length=6):
    char1 = '0123456789'
    char21 = 'abcdefghijklmnopqrstuvwxyz'
    char22 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    char3 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    char4 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    char5 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_+=-*/~'

    c = char5
    if not ismix:
        c = char4
    if isalpha:
        c = char3
        if not isupper:
            c = char21
        if alluper:
            c = char22

    if isdigit:
        c = char1
    return ''.join([c[random.randint(0, len(c)-1)] for _ in range(length)])


def hash_string(src_string: str, salt: dict = None):
    hasher = hashlib.md5(src_string.encode('utf8'))
    if salt:
        for k, v in salt.items():
            hasher.update(v.encode('utf8'))
    return hasher.hexdigest()


def hump2snake(hump: str):
    return re.sub(r'([a-z]|\d)([A-Z])', r'\1_\2', hump).lower()


def snake2hump(snake: str):
    return re.sub(r'(_\w)', lambda x: x.group(1)[1].upper(), snake)

